<?php
$configModel = new RM_Config();
$dateFormat = $configModel->getJSDateformat();

$config = new stdClass();

if ($dateFormat == "d/m/Y"){
    $lng_dateFormat = "dd/mm/yyyy";
} else {
    $lng_dateFormat = "mm/dd/yyyy";
}

$criteria = RM_Reservation_Manager::getInstance()->getCriteria(); // get any saved dates
if (isset($criteria) && isset($criteria->start_datetime) && isset($criteria->end_datetime)){
    $config->start_datetime = $criteria->start_datetime ? date($dateFormat, strtotime($criteria->start_datetime)) : $lng_dateFormat;
    $config->end_datetime = $criteria->end_datetime ? date($dateFormat, strtotime($criteria->end_datetime)) : $lng_dateFormat;
} else {
    $config->start_datetime = $lng_dateFormat;
    $config->end_datetime = $lng_dateFormat;
}

$config->dp_dateformat = $dateFormat;
$config->dp_view_dateformat = $lng_dateFormat;

if (!class_exists("RM_DailyTimePricesConfig")){
    ?>
    <div id="<?php echo $this->panel->getDivID() ?>">
        !!!Daily Time Prices module not available - Please remove this panel and add the standard date selection!!!
    </div>
    <?php
} else {

$dailyTimePricesConfigObject = new RM_DailyTimePricesConfig();
$startTime = $dailyTimePricesConfigObject->fetchRow("name = 'default_end_time'");
$endTime = $dailyTimePricesConfigObject->fetchRow("name = 'default_start_time'" );
$timeSelInfo = array('default_end_time'=>$startTime->value,'default_start_time'=>$endTime->value);

?>
<div id="<?php echo $this->panel->getDivID() ?>">

    <div class="RM_advanced_search_dates_container">

        <input id="RM_advanced_search_startdatetime_hidden" name="search[start_datetime]" type="hidden" value="<?php echo $config->start_datetime;?>" />
        <input id="RM_advanced_search_enddatetime_hidden" name="search[end_datetime]" type="hidden" value="<?php echo $config->end_datetime;?>" />

        <table width="90%" border="0" cellpadding="0" cellspacing="0" align="center" class="RM_advancedsearch_formrow">
            <tr>
                <td class="RM_advancedsearch_leftcolumn">
                    <?php echo $this->_translate->_('User.Advancedsearch.Main','StartDate');?>
                </td>
                <td>
                    <table width="100%" border="0">
                        <tr>
                            <td nowrap>
                                <div id="RM_advanced_search_start_calendar"></div>
                                <input id="RM_advanced_search_startdatetime" type="text" value="<?php echo $config->start_datetime;?>" class="RM_advanced_search_text_box" onfocus="RM_advanced_search_hideDefault(this)" />
                                <input type="button" id="advancedSearchOpenStartCalendar" value="" class="datepicker" />
                            </td>
                            <td nowrap>
                                <div id="RM_advanced_search_starttime"></div>
                            </td>
                        </tr>
                    </table>


                </td>
            </tr>
            <tr>
                <td class="RM_advancedsearch_leftcolumn">
                    <?php echo $this->_translate->_('User.Advancedsearch.Main','EndDate');?>
                </td>
                <td>
                    <table width="100%" border="0">
                        <tr>
                            <td nowrap>
                                <div id="RM_advanced_search_end_calendar"></div>
                                <input id="RM_advanced_search_enddatetime" type="text" value="<?php echo $config->end_datetime;?>" class="RM_advanced_search_text_box" onfocus="RM_advanced_search_hideDefault(this)" />
                                <input type="button" id="advancedSearchOpenEndCalendar" value="" class="datepicker" />
                            </td>
                            <td nowrap>
                                <div id="RM_advanced_search_endtime"></div>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>

    </div>
</div>

<script type="text/javascript">
    var RM_advanced_search_viewFormat = '<?php echo $config->dp_dateformat; ?>';
    var RM_advanced_search_viewRenderFormat = '<?php echo $config->dp_view_dateformat; ?>';
    var RM_advanced_search_submitFormat = 'Y-m-d';

    var RM_advanced_search_normalStyle = {
        backgroundColor: 'white',
        color: 'black'
    }

    var RM_advanced_search_errorStyle = {
        backgroundColor: 'red',
        color: 'black'
    }

    var RM_advanced_search_defaultStyle = {
        backgroundColor: 'white',
        color: 'grey'
    }

    function RM_advanced_search_applyStyle(input, style){
        input.style.backgroundColor = style.backgroundColor;
        input.style.color = style.color;
    }

    function RM_advanced_search_hideDefault(selectedInput){
        if (selectedInput.value == RM_advanced_search_viewRenderFormat) {
            RM_advanced_search_applyStyle(selectedInput, RM_advanced_search_normalStyle);
            selectedInput.value = '';
        }
    }

    function RM_advanced_search_submit() {


        var startdate_field = document.getElementById('RM_advanced_search_startdatetime');
        var startdate_field_hidden = document.getElementById('RM_advanced_search_startdatetime_hidden');
        var enddate_field = document.getElementById('RM_advanced_search_enddatetime');
        var enddate_field_hidden = document.getElementById('RM_advanced_search_enddatetime_hidden');

        // keep track of what form items we need to submit
        RM_advanced_search_form_items.push('RM_advanced_search_startdatetime_hidden');
        RM_advanced_search_form_items.push('RM_advanced_search_enddatetime_hidden');

        //If both start and end date sets to empty strings or set to default template values
        if ((startdate_field.value == '' || startdate_field.value == RM_advanced_search_viewRenderFormat) &&
            (enddate_field.value == '' || enddate_field.value == RM_advanced_search_viewRenderFormat)) {

            startdate_field_hidden.value = '';
            enddate_field_hidden.value = '';
            //document.getElementById('RM_advanced_search').submit();
            return true;
        }

        //If stardate is empty
        if ((startdate_field.value == '' || startdate_field.value == RM_advanced_search_viewFormat)){
            RM_advanced_search_applyStyle(startdate_field, RM_advanced_search_errorStyle);
            return false;
        }

        //If enddate is empty
        if (enddate_field.value == '' || enddate_field.value == RM_advanced_search_viewFormat) {
            RM_advanced_search_applyStyle(enddate_field, RM_advanced_search_errorStyle);
            return false;
        }

        var success = true;
        var startDate = Date.parseDate(startdate_field.value, RM_advanced_search_viewFormat);
        if (!startDate) {
            RM_advanced_search_applyStyle(startdate_field, RM_advanced_search_errorStyle);
            success = false;
        } else {
            RM_advanced_search_applyStyle(startdate_field, RM_advanced_search_normalStyle);
        }

        var endDate = Date.parseDate(enddate_field.value, RM_advanced_search_viewFormat);
        if (!endDate) {
            RM_advanced_search_applyStyle(enddate_field, RM_advanced_search_errorStyle);
            return false;
        } else {
            RM_advanced_search_applyStyle(enddate_field, RM_advanced_search_normalStyle);
        }

        if (success && startDate.getTime() > endDate.getTime()) {
            RM_advanced_search_applyStyle(enddate_field, RM_advanced_search_errorStyle);
            return false;
        }

        if (success) {
            startdate_field_hidden.value = startDate.format(RM_advanced_search_submitFormat) + " " + Ext.getCmp('rm_time_starttime').value;
            enddate_field_hidden.value = endDate.format(RM_advanced_search_submitFormat) + " " + Ext.getCmp('rm_time_endtime').value;
            //document.getElementById('RM_advanced_search').submit();
            return true;
        }

        return false;
    }

    function rm_advanced_search_init() {
        if (typeof(RM_advanced_search_submit_event_handlers) !== 'undefined') {
            RM_advanced_search_submit_event_handlers.push(RM_advanced_search_submit);
        }

        var startdate_field = document.getElementById('RM_advanced_search_startdatetime');
        if (startdate_field.value == RM_advanced_search_viewFormat) {
            RM_advanced_search_applyStyle(startdate_field, RM_defaultStyle);
        } else {
            RM_advanced_search_applyStyle(startdate_field, RM_advanced_search_normalStyle);
        }

        var enddate_field = document.getElementById('RM_advanced_search_enddatetime');
        if (enddate_field.value == RM_advanced_search_viewFormat) {
            RM_advanced_search_applyStyle(enddate_field, RM_defaultStyle);
        } else {
            RM_advanced_search_applyStyle(enddate_field, RM_advanced_search_normalStyle);
        }

        var start_advanced_selectHandler = function(datepicker, date) {
            var startdate_field = document.getElementById('RM_advanced_search_startdatetime');
            startdate_field.value = date.format(RM_advanced_search_viewFormat);
            RM_advanced_search_applyStyle(startdate_field, RM_advanced_search_normalStyle);
            datepicker.hide();

            var enddate_field = document.getElementById('RM_advanced_search_enddatetime');
            if (enddate_field.value == '' || enddate_field.value == RM_advanced_search_viewFormat) {
                enddate_field.value = date.format(RM_advanced_search_viewFormat);
                RM_advanced_search_applyStyle(enddate_field, RM_advanced_search_normalStyle);
            }
        };

        var end_advanced_selectHandler = function(datepicker, date) {
            var enddate_field = document.getElementById('RM_advanced_search_enddatetime');
            enddate_field.value = date.format(RM_advanced_search_viewFormat);
            RM_advanced_search_applyStyle(enddate_field, RM_advanced_search_normalStyle);
            datepicker.hide();
        };

        var RM_DP_ADVANCED_StartDate = new Ext.DatePicker({
            startDay: 1,
            listeners: {
                'select': start_advanced_selectHandler
            }
        });
        var RM_DP_ADVANCED_EndDate = new Ext.DatePicker({
            startDay: 1,
            listeners: {
                'select': end_advanced_selectHandler
            }
        });

        // time selection fields
        var RM_DP_ADVANCED_StartTime = new Ext.form.TimeField({
            id: "rm_time_starttime",
            width: 80,
            increment: 15,
            format: "H:i",
            value: '<?php echo (!empty($timeSelInfo) ? $timeSelInfo['default_start_time'] : '9:00'); ?>'
        });



        var RM_DP_ADVANCED_EndTime = new Ext.form.TimeField({
            id: "rm_time_endtime",
            width: 80,
            increment: 15,
            format: "H:i",
            value: '<?php echo (!empty($timeSelInfo) ? $timeSelInfo['default_end_time'] : '17:00'); ?>'
        });

        RM_DP_ADVANCED_EndTime.render('RM_advanced_search_endtime');
        RM_DP_ADVANCED_StartTime.render('RM_advanced_search_starttime');

        RM_DP_ADVANCED_StartDate.render('RM_advanced_search_start_calendar');
        RM_DP_ADVANCED_EndDate.render('RM_advanced_search_end_calendar');

        //hide date picker straight away
        RM_DP_ADVANCED_StartDate.hide();
        RM_DP_ADVANCED_EndDate.hide();

        //define click handler
        var startDate_advanced_clickHandler = function() {
            RM_DP_ADVANCED_StartDate.show();
        };

        var endDate_advanced_clickHandler = function() {
            RM_DP_ADVANCED_EndDate.show();
        };

        //add listener for button click
        Ext.EventManager.on('advancedSearchOpenStartCalendar', 'click', startDate_advanced_clickHandler);
        Ext.EventManager.on('advancedSearchOpenEndCalendar', 'click', endDate_advanced_clickHandler);
    };

    rm_advanced_search_init();
</script>
<?php } ?>